bugs.feeds = {
};

bugs.feeds.artistComments = bugs.create();
bugs.feeds.artistComments.prototype = {
	PAGE_ROW : 5,
	URL : g_urlMe + "/ajax/inbox/comments",
	initialize : function(total) {
		this.page = 1;
		this.total = eval(total);
		this.pageCount = bugs.math.ceil(this.total / this.PAGE_ROW);
		this.box = $(".latelyArtistComment"); 
	},
	nextPage : function() {
		if(this.page >= this.pageCount) return;
		this.load(this.page + 1);
	},
	prevPage : function() {
		if(this.page <= 1) return;
		this.load(this.page - 1);
	}, 
	load : function(page) {
		
		$.get(this.URL, {page : page, row : this.PAGE_ROW}, $.proxy(function(result) {
			if(typeof result != "string" ) return;
			var list = $(result);
			if(list.length == 0 || list.get(0).tagName != "LI") return;
			
			$("ul *", this.box).remove();
			$("ul", this.box).append(list);
			this.page = page;
			$(".pagination .page", this.box).text(this.page + "/" + this.pageCount);
			if(this.page >= this.pageCount) {
				$(".pagination .btnArea a:first", this.box).show();
				$(".pagination .btnArea a:last", this.box).hide();
			} else if(this.page <= 1) {
				$(".pagination .btnArea a:first", this.box).hide();
				$(".pagination .btnArea a:last", this.box).show();
			} else
				$(".pagination .btnArea a", this.box).show();
			
		}, this));
	}
} 


bugs.feeds.noticeList = bugs.create();
bugs.feeds.noticeList.prototype = {
	PAGE_ROW : 20,
	URL : g_urlMe + "/ajax/inbox/notices",
	checkboxes : [],
	initialize : function(total) {
		this.page = 1;
		this.total = eval(total);
		this.pageCount = bugs.math.ceil(this.total / this.PAGE_ROW);
		this.box = $(".titleArea.receiveNotice").parent(); 
		this.initCheck();
	},
	initCheck : function() {
		this.appendList($("ul.receiveNotice li", this.box));
		
		var _checks = this.checkboxes;
		this.checkbox = new bugs.form.checkbox($(".onclickAction div.check"), "checkAll", {
			checked : function() {
				for(var i=0;i<_checks.length;i++) {
					_checks[i].checked();
				}
			},
			unchecked : function() {
				for(var i=0;i<_checks.length;i++) {
					_checks[i].unchecked();
				}
			}
		});
		this.checkboxes = _checks;
	},
	appendList : function(liObj) {
		var _checks = this.checkboxes;
		var _this = this;
		liObj.each(function() {
			if(this.tagName != "LI") return;
			var li = $(this);
			_checks.push( new bugs.form.checkbox(li.find("div.check"), "_chk"+name, {
				checked : function() { li.addClass("rowSelect"); },
				unchecked : function() { li.removeClass("rowSelect"); }
			}));
		}).one("click", function(event) {
				var li = $(this);
				// prevent checkbox, delete button event
				if($(".check, .check *, .delete, .delete *, .thumbnail, .thumbnail *", li).index(event.target) > -1) return;
				li.find(".newsContent").show().end().addClass("expand");
				//notice read
				if(li.hasClass("readNo")) {
					_this.read($(this).find("input:checkbox").val(), li);
				}
		});
	},
	more : function() {
		if(this.page >= this.pageCount) return;
		this.load(this.page + 1);
	},
	read : function(noticeId, list) {
		$.post(g_urlMe + "/ajax/inbox/readNotice", {"notice_id" : noticeId}, function(result) {
			if(typeof result == "string" && result =="1") {
				list.removeClass("readNo");
			}
		});
		
	},
	load : function(page) {
		
		$.get(this.URL, {page : page, row : this.PAGE_ROW}, $.proxy(function(result) {
			if(typeof result != "string" ) return;
			var list = $(result);
			if(list.length == 0 || list.get(0).tagName != "LI") return;
			
			$("ul", this.box).append(list).find("li");
			// 새로생기는 checkbox, list init
			this.appendList(list);
			this.page = page;
			bugs.log.log(this.page);
			bugs.log.log(this.pageCount);
			if(this.page >= this.pageCount) {
				$(".pagination .btnArea a:first", this.box).show();
				$(".moreList", this.box).hide();
			}
			
		}, this));
	},
	del : function(notice_id) {
		var chk;
		if(typeof notice_id == "undefined") {
			chk = $(".list.receiveNotice li input:checkbox[name=notice_id]:checked");
			if(chk.length == 0) {
				bugs.ui.showAlert("삭제할 알림을 선택하여 주십시오.", {});
				return;
			}
		} else {
			$(".list.receiveNotice li input:checkbox[name=notice_id]").each(function() {this.checked = false;});
			chk = $(".list.receiveNotice li input:checkbox[name=notice_id][value="+notice_id+"]").each(function() {this.checked = true;});
		}
		 
		bugs.ui.showAlert("삭제 하시겠습니까?", {css : "layerAdultNotice", onOk : function(popup) {
			var frm = $("form[nmae=noticeList]").get(0);
			frm.method = "post";
			//frm.action = g_urlMe + "/inbox/notifications";
			frm.action = g_urlMe + "/ajax/inbox/deleteNotices";
			frm.submit();
		}, onCancel : function() {
			
		}});
		_layer.show();
			
		//if(confirm("삭제 하시겠습니까?")==false) return;
		
		/*
		var noticeIds = [];
		chk.each(function() {
			bugs.log.log(this);
			noticeIds.push(this.value);
		});
		var url = g_urlMe + "/ajax/inbox/deleteNotices";
		$.get(url, {"ids[]" : noticeIds}, $.proxy(function(result) {
				this.load(this.page);				
			}, this)
		);
		*/			
	}
} 



bugs.feeds.artistActivities = bugs.create();
bugs.feeds.artistActivities.prototype = {
	PAGE_ROW : 10,
	URL : g_urlMe + "/ajax/inbox/artists",
	initialize : function(total) {
		this.page = 1;
		this.total = eval(total);
		this.pageCount = bugs.math.ceil(this.total / this.PAGE_ROW);
		this.box = $(".titleArea.artistNews").parent(); 
	},
	more : function() {
		if(this.page >= this.pageCount) return;
		this.load(this.page + 1);
	},
	load : function(page) {
		$.get(this.URL, {page : page, row : this.PAGE_ROW}, $.proxy(function(result) {
			if(typeof result != "string" ) return;
			var list = $(result);
			if(list.length == 0 || list.get(0).tagName != "LI") return;
			$("ul", this.box).append(list).find("li");
			this.page = page;
			if(this.page >= this.pageCount) {
				$(".pagination .btnArea a:first", this.box).show();
				$(".moreList", this.box).hide();
			}
			
		}, this));
	}
} 



bugs.feeds.myHistories = bugs.create();
bugs.feeds.myHistories.prototype = {
	PAGE_ROW : 5,
	URL : g_urlMe + "/ajax/myhistories",
	initialize : function(total, msrl) {
		this.page = 1;
		this.total = eval(total);
		this.pageCount = bugs.math.ceil(this.total / this.PAGE_ROW);
		this.box = $(".activity");
		this.msrl = msrl;
	},
	nextPage : function() {
		if(this.page >= this.pageCount) return;
		this.load(this.page + 1);
	},
	prevPage : function() {
		if(this.page <= 1) return;
		this.load(this.page - 1);
	}, 
	load : function(page) {
		$.get(this.URL, {msrl : this.msrl, page : page, row : this.PAGE_ROW}, $.proxy(function(result) {
			if(typeof result != "string" ) return;
			var list = $(result);
			if(list.length == 0 || list.get(0).tagName != "LI") return;
			
			$("ul *", this.box).remove();
			$("ul", this.box).append(list);
			this.page = page;
			$(".pagination .page", this.box).text(this.page + "/" + this.pageCount);
			if(this.page >= this.pageCount) {
				$(".pagination .btnArea a:first", this.box).show();
				$(".pagination .btnArea a:last", this.box).hide();
			} else if(this.page <= 1) {
				$(".pagination .btnArea a:first", this.box).hide();
				$(".pagination .btnArea a:last", this.box).show();
			} else
				$(".pagination .btnArea a", this.box).show();
			
		}, this));
	},
	
	myHistorydel : function (myHistoryId, callbackTrue, callbackFalse)
    {
		$.post(g_urlAjax+"/user/history/deleteHistory", {myHistoryId : myHistoryId} , function(ret) {
			if (ret) {
				if(typeof(callbackTrue) == "string") eval(callbackTrue);
				else if(typeof(callbackTrue) == "function") callbackTrue(ret);
			}
			else {
				if(data.error_type == "NOT_LOGGED")	bugs.ui.showLoginLayer();
				
				if(typeof(callbackFalse) == "string") eval(callbackFalse);
				else if(typeof(callbackFalse) == "function") callbackFalse(ret);
			}
		});
    }
} 
